package org.example.demo.listen;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.example.demo.entity.Student;
import org.example.demo.service.StudentService;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


@Slf4j
public class StudentExcelListener extends AnalysisEventListener<Student> {

    public StudentService studentService;
    public StudentExcelListener() {}
    public StudentExcelListener(StudentService studentService) {
        this.studentService = studentService;
    }

    private static final int BATCH_COUNT = 100;
    private List<Student> dataList = new ArrayList<>();


    //一行一行读取excel内容(从第二行开始读取,第一行是表头,该方法不会读取第一行)
    @Override
    public void invoke(Student student, AnalysisContext analysisContext) {

        log.info("解析到一条数据:{}", JSONObject.toJSON(student));
        dataList.add(student);

        if (dataList.size() >= BATCH_COUNT) {

            dataList.clear();


        }
    }

    //读取完成之后执行这个方法
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            log.info("所有数据解析完成！");

    }









    }



